#include <cstdio>
#include <cstring>
#define N 110
using namespace std;
int n;

int e_cnt, heads[N];
struct Edge {
	int v, nxt;
} e[N << 1];
inline void add(int u, int v) {
	e[++e_cnt].v = v;
	e[e_cnt].nxt = heads[u];
	heads[u] = e_cnt;
}
inline int max(int a, int b) { return a > b ? a : b; }
inline void swap(int &a, int &b) {
	int ttt = a;
	a = b, b = ttt;
}
int size[N], a[N], deep[N], fa[N], dfn[N], cnt = 0;
int hs[N], top[N], tmp[N], tmp2[N], f[N], hea2 = 0, hea = 0;

void dfs1(int x) {
	hs[x] = 0;
	size[x] = 1;
	for (int i = heads[x]; i; i = e[i].nxt) {
		if (e[i].v == fa[x]) continue;
		deep[e[i].v] = deep[x] + 1;
		fa[e[i].v] = x;
		dfs1(e[i].v);
		size[x] += size[e[i].v];
		if (size[e[i].v] > size[hs[x]]) hs[x] = e[i].v;
	}
}

void dfs2(int x, int topp) {
	dfn[x] = ++cnt;
	top[x] = topp;
	if (hs[x]) { dfs2(hs[x], topp); }
	for (int i = heads[x]; i; i = e[i].nxt) {
		if (e[i].v == fa[x] || e[i].v == hs[x]) continue;
		dfs2(e[i].v, e[i].v);
	}
}
inline int work(int x, int y) {
	while (top[x] != top[y]) {
		if (deep[top[x]] < deep[top[y]]) swap(x, y);
		x = fa[top[x]];
	}
	if (deep[x] < deep[y]) swap(x, y);
	return y;
}
inline int query(int s, int t) {
	memset(f, 0, sizeof(f));
	hea = 0, hea2 = 0;
	register int lca = work(s, t);
	while (s != lca) {
		tmp[++hea] = a[s];
		for (register int i = heads[s]; i; i = e[i].nxt) {
			register int v = e[i].v;
			if (deep[v] == deep[s] - 1) {
				s = v;
				break;
			}
		}
	}
	tmp[++hea] = a[lca];
	while (t != lca) {
		tmp2[++hea2] = a[t];
		for (register int i = heads[t]; i; i = e[i].nxt) {
			register int v = e[i].v;
			if (deep[v] == deep[t] - 1) {
				t = v;
				break;
			}
		}
	}
	while (hea2) { tmp[++hea] = tmp2[hea2--]; }
	for (register int i = 1; i <= hea; ++i)
		for (register int j = 0; j < i; ++j) {
			f[i] = max(f[i], f[i - 1]);
			if (tmp[i] > tmp[j]) f[i] = max(f[i], f[j] + 1);
		}
	register int res = 0;
	for (register int i = 1; i <= hea; ++i) res = max(res, f[i]);
	return res;
}
int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);
#endif
#endif

	scanf("%d", &n);
	int s, t;
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &s, &t);
		add(s, t);
		add(t, s);
	}
	dfs1(1);
	dfs2(1, 1);
	int q, x, y, z;
	for (register int i = 1; i <= n; ++i) { scanf("%d", &a[i]); }
	scanf("%d%d%d%d%d%d", &q, &s, &t, &x, &y, &z);
	int ans = 0;
	ans += query(s, t);
	for (register int i = 1; i < q; ++i) {
		s = (x * s * s + y * s + z) % n + 1, t = (x * t * t + y * t + z) % n + 1;
		if (s != t)
			ans += query(s, t);
		else {
			++ans;
		}
	}
	printf("%d", ans);
	return 0;
}